package com.book.iphone;

import com.book.book.domain.BooksDO;
import com.book.book.domain.UsersDO;
import com.book.book.domain.WantSeeDO;
import com.book.book.service.*;
import com.book.iphone.domain.CateBook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 手机页面控制器
 * Created by siyuan on 2018/1/28.
 */

@RequestMapping("/iphone")
@Controller
public class indexController {

    @Autowired
    private BooksService booksService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BookService bookService;
    @Autowired
    private WantSeeService wantSeeService;
    @Value("${book.redirect}")
    private String redirect;

    /**
     * 主页面控制器
     *
     * @param model
     * @return
     */
    @GetMapping()
    public String index(HttpServletRequest request, Model model) {
        Map map = new HashMap();
        map.put("limit", 2);
        map.put("offset", 0);
        map.put("order", "bookId");
        List<BooksDO> bookDOList = booksService.list(map);
        if (bookDOList.size() > 0) {
            for (BooksDO b : bookDOList) {
                String img1 = b.getBookImg().split(",") == null ? "/img/about-zuixin-02.jpg" : b.getBookImg().split(",")[0];
                b.setBookImg(img1);
            }
            model.addAttribute("booklist", bookDOList);
        }else {
            model.addAttribute("booklist", null);
        }

        /**
         * 最新漂流
         * 取出最近新上架的书
         *
         */
        Map map1 = new HashMap();
        //取出的条数
        map1.put("limit", 2);
        //取出的页数
        map1.put("offset", 0);
        //取出的分类
        // map1.put("", "");
        List<BooksDO> bookDOList1 = booksService.list(map1);
        if (bookDOList1.size() > 0) {
            for (BooksDO b : bookDOList1) {
                String img1 = b.getBookImg().split(",") == null ? "/img/about-zuixin-02.jpg" : b.getBookImg().split(",")[0];
                b.setBookImg(img1);
            }
            model.addAttribute("newbook", bookDOList1);
        }else {
            model.addAttribute("newbook", null);
        }

        /***
         * 心愿列表
         */
        Map map2 = new HashMap();

        map2.put("limit", 3);
        map2.put("offset", 0);
        List<WantSeeDO> wantSeeDOList=wantSeeService.list(map2);
        if (wantSeeDOList!=null||wantSeeDOList.size()!=0){
            model.addAttribute("wanbook", wantSeeDOList);
        }else {
            model.addAttribute("wanbook", null);
        }
        return "book/phone/main/home";
        // return "login";
    }

    /**
     * 获取用户个人信息
     *
     * @param model
     * @return
     */
    @GetMapping("/user")
    String getUseInfo(Model model, HttpServletRequest request) {
        UsersDO usersDO = (UsersDO) request.getSession().getAttribute("userInfo");
        if (usersDO == null) {
            return "redirect:/iphone/login";
        } else {
            String userId = usersDO.getUserId();
            usersDO = usersService.getopenid(usersDO.getUserId());
            request.getSession().setAttribute("userInfo", usersDO);
            model.addAttribute("userinfo", usersDO);
        }
        return "book/phone/main/person";
    }


    /***
     * 图书交流
     * 查询出图书交流 的列表
     * @param model
     * @return
     */
    @GetMapping("/social")
    String social(HttpServletRequest request,Model model) {
        UsersDO usersDO = (UsersDO) request.getSession().getAttribute("userInfo");
        if (usersDO==null){
            return "redirect:/iphone/login";
        }
        model.addAttribute("usersDO",usersDO);
        return "book/phone/html/communciate";
    }
    /**
     * 跳转 图书分类
     *
     * @return
     */
    @GetMapping("/stack")
    String stack(Model model) {
        /**
         * 返回出分类的图书
         */
        List<CateBook> cateBookList = bookService.selectBookListWithCate();
        if (cateBookList.size() > 0) {
            for (CateBook c : cateBookList) {
                if (c.getBookImg() != null) {
                    String img1 = c.getBookImg().split(",") == null ? "/img/about-zuixin-02.jpg" : c.getBookImg().split(",")[0];
                    c.setBookImg(img1);
                } else {
                    c.setBookImg("/img/about-zuixin-02.jpg");
                    //continue;
                }
            }
        }
        //  Map<String,List<CateBook>> CateBookMap=new HashMap<>();
        Map<String, List<CateBook>> CateMap = new HashMap<>();
        for (int i = 0; i < cateBookList.size(); i++) {
            String key = cateBookList.get(i).getCatName();
            boolean flag = CateMap.containsKey(key);
            if (flag) {
                CateMap.get(key).add(cateBookList.get(i));
            } else {
                List<CateBook> book = new ArrayList<>();
                book.add(cateBookList.get(i));
                CateMap.put(key, book);
            }
        }
        model.addAttribute("CateMap", CateMap);
        return "book/phone/main/stack";
    }
//    @RequestMapping("/wishList")
//    String wishList(){
//        return "book/phone/html/wash_list";
//    }
}
